define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'auth/admin/index',
                    add_url: 'auth/admin/add',
                    edit_url: 'auth/admin/edit',
                    del_url: 'auth/admin/del',
                    multi_url: 'auth/admin/multi',
                    resetpwd_url: 'auth/admin/resetpwd',
                },
            });

            var table = $("#table");

            //在表格内容渲染完成后回调的事件
            table.on('post-body.bs.table', function (e, json) {
                $("tbody tr[data-index]", this).each(function () {
                    if (parseInt($("td:eq(1)", this).text()) == Config.admin.id) {
                        $("input[type=checkbox]", this).prop("disabled", true);
                    }
                });
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                columns: [
                    [
                        {field: 'state', checkbox: true, },
                        {field: 'id', title: 'ID', operate:false},
                        {
                            field: 'department_id',
                            title: __('部门'),
                            visible: false,
                            addclass: 'selectpage',
                            extend: 'data-source="department/index/selectpage" data-field="name"',
                            operate: 'in',
                            formatter: Table.api.formatter.search
                        },
                        {field: 'department_name', title: __('部门'), formatter: Table.api.formatter.label, operate: false},
                        {field: 'position.name', title: __('职务'), formatter: Table.api.formatter.label},
                        // {field: 'grouping_text', title: __('小组'), operate:false, formatter: Table.api.formatter.label},
                        {field: 'type.name', title: __('密级'), formatter: Table.api.formatter.label},
                        {field: 'username', title: __('Username'), operate:false},
                        {field: 'nickname', title: __('姓名'), operate:'like'},
                        {field: 'groups_text', title: __('后台权限组'), operate:false, formatter: Table.api.formatter.label},
                        // {field: 'email', title: __('Email')},
                        // {field: 'mobile', title: __('Mobile')},
                        {field: 'status', title: __("Status"), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},
                        {field: 'logintime', title: __('Login time'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'resetpwd',
                                    text: __('重置密码'),
                                    title: __('重置密码'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-magic',
                                    url: $.fn.bootstrapTable.defaults.extend.resetpwd_url,
                                    confirm: '确认重置？',
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                            ], formatter: function (value, row, index) {
                                if(row.id == Config.admin.id){
                                    return '';
                                }
                                return Table.api.formatter.operate.call(this, value, row, index);
                            }}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            $(document).on("click", ".btn-resetpwd", function (e) {
                var ids =  Table.api.selectedids(table); //取出来是个数组  示例：[1,2,3,4,5]
                ids = ids.join(','); //用逗号隔开  数组分割后：1,2,3,4,5
                console.log(ids);

                Layer.confirm(__('确认重置这些人的密码为:【123456】？'), {
                    title: __('Warmtips'),
                    btn: [__('确认'), __('取消')]
                }, function (index) {
                    Fast.api.ajax({
                        url: $.fn.bootstrapTable.defaults.extend.resetpwd_url,
                        data: {ids: ids},
                        loading: false,
                    });
                    layer.close(index);
                }, function (index) {
                    layer.close(index);
                });

            });

        },
        add: function () {
            $(document).on("change", "#c-group", function(){
                var val = $(this).val();
                if (val == 1) {
                    $(".c-type_id").hide();
                    $(".c-department_id").hide();
                    $(".c-position_id").hide();
                } else {
                    $(".c-type_id").show();
                    $(".c-department_id").show();
                    $(".c-position_id").show();
                }
            });
            Form.api.bindevent($("form[role=form]"));
        },
        edit: function () {
            $(document).on("change", "#c-group", function(){
                var val = $(this).val();
                if (val == 1) {
                    $(".c-type_id").hide();
                    $(".c-department_id").hide();
                    $(".c-position_id").hide();
                } else {
                    $(".c-type_id").show();
                    $(".c-department_id").show();
                    $(".c-position_id").show();
                }
            });
            Form.api.bindevent($("form[role=form]"));
        },
        excel:function ()  {
            Form.api.bindevent($("form[role=form]"));
        },
    };
    return Controller;
});
